package com.ljfiction.book.activitys;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.androidkun.xtablayout.XTabLayout;
import com.ljfiction.book.Dialog.NoBackgLodDialog;
import com.ljfiction.book.MyView.BadgeView;
import com.ljfiction.book.R;
import com.ljfiction.book.adapter.IndentDetailAdapter;
import com.ljfiction.book.adapter.SimpleFragmentMyauthorMessagePageAdapter;
import com.ljfiction.book.fragment.MyBookshupingFragment;
import com.ljfiction.book.fragment.MyComplanFragment;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.myinterface.MyAuthorMessageInterface;
import com.ljfiction.book.tools.AppUtils;
import com.ljfiction.book.tools.ScreenUtils;
import com.ljfiction.book.tools.SettingManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class MyComplaintsActivity extends BaseActivity {

    @BindView(R.id.back_mycomplan)
    ImageView backMycomplan;
    @BindView(R.id.xtablayout_mycomplain)
    XTabLayout xtablayoutMycomplain;
    @BindView(R.id.viewpager_mycomplain)
    ViewPager viewpagerMycomplain;

    private SimpleFragmentMyauthorMessagePageAdapter adapter;
    private List<Integer> mBadgeCountList = new ArrayList<>();
    private List<BadgeView> mBadgeViews;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private String tabTitles[] = new String[]{"书评", "吐槽"};
    private String bookId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_complaints);
        ButterKnife.bind(this);
        AppUtils.init(this);
        initBadgeViews();
        bookId = getIntent().getStringExtra("bookId");
        intView();
    }

    private void intView() {
        MyBookshupingFragment myBookshupingFragment = MyBookshupingFragment.newInstance(bookId).setLisener(myAuthorMessageInterface);
        MyComplanFragment myComplanFragment = MyComplanFragment.newInstance(bookId).setLisener(myAuthorMessageInterface);
        fragments.add(myBookshupingFragment);
        fragments.add(myComplanFragment);
        mBadgeCountList.add(getIntent().getIntExtra("CommCounts", 0));
        mBadgeCountList.add(getIntent().getIntExtra("ParCounts", 0));
        adapter = new SimpleFragmentMyauthorMessagePageAdapter(this, getSupportFragmentManager(), fragments, tabTitles, mBadgeCountList);
        IndentDetailAdapter adapter = new IndentDetailAdapter(getSupportFragmentManager(), tabTitles, fragments);
        viewpagerMycomplain.setAdapter(adapter);
        viewpagerMycomplain.setOverScrollMode(View.OVER_SCROLL_NEVER);
        xtablayoutMycomplain.setupWithViewPager(viewpagerMycomplain);
        setUpTabBadge();
    }

private void  toonekeyread(int type){
    OkHttpUtils.post(HttpManager.IP+"/allCommentRead")

            .params("bookId",bookId)
            .params("type",String.valueOf(type))
            .execute(new StringCallback() {


                @Override
                public void onSuccess(String s, Call call, Response response) {

                    try {
                        JSONObject js=new JSONObject(s);
                        showToast(js.getString("msg"));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            });
}
    private void setUpTabBadge() {
        // 1. 最简单
       /* for (int i = 0; i < fragments.size(); i++) {
            mBadgeViews.get(i).setTargetView(((ViewGroup) myinformationxtable.getChildAt(0)).getChildAt(i));
            mBadgeViews.get(i).setText(formatBadgeNumber(mBadgeCountList.get(i)));
        }*/
        // 2. 最实用
        for (int i = 0; i < fragments.size(); i++) {
            XTabLayout.Tab tab = xtablayoutMycomplain.getTabAt(i);
            // 更新Badge前,先remove原来的customView,否则Badge无法更新
            assert tab != null;
            View customView = tab.getCustomView();
            if (customView != null) {
                ViewParent parent = customView.getParent();
                if (parent != null) {
                    ((ViewGroup) parent).removeView(customView);
                }
            }
            // 更新CustomView
            tab.setCustomView(adapter.getTabItemView(i));
        }
        // 需加上以下代码,不然会出现更新Tab角标后,选中的Tab字体颜色不是选中状态的颜色
        //myinformationxtable.getTabAt(myinformationxtable.getSelectedTabPosition()).getCustomView().setSelected(true);
    }

    private void initBadgeViews() {
        if (mBadgeViews == null) {
            mBadgeViews = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                BadgeView tmp = new BadgeView(this);
                tmp.setBadgeMargin(0, 6, 10, 0);
                tmp.setTextSize(ScreenUtils.getNum(this, 10));
                mBadgeViews.add(tmp);
            }
        }
    }

    @OnClick({R.id.back_mycomplan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_mycomplan:
                goBack();
                break;

        }
    }

    public MyAuthorMessageInterface myAuthorMessageInterface = num -> {
        switch (num) {
            case 2:
                mBadgeCountList.set(0, 0);
                toonekeyread(1);
                break;
            case 3:
                mBadgeCountList.set(1, mBadgeCountList.get(1) - 1);
                break;
            case 4:
                mBadgeCountList.set(1, 0);
                toonekeyread(2);
                break;
                default:
                    mBadgeCountList.set(0, mBadgeCountList.get(0) - 1);
                    todeletOne(num);
                    break;
        }
        setUpTabBadge();

    };

    private void todeletOne(int num) {
        OkHttpUtils.post(HttpManager.IP+"/updateCommentReadByID")
                .params("commentId",String.valueOf(num))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                    }
                });

    }

}
